package ymos.mytracker.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import ymos.mytracker.exception.ServerInfoExcption;
import ymos.mytracker.json.ServInfo;
import ymos.mytracker.service.GetInfoService;

@RestController
@RequestMapping("csgo")
public class WebController {
	@Autowired
	GetInfoService getinfo;
	@PostMapping("getServerInfo")
	public ServInfo getServerinfo(String ip,String port,HttpServletResponse resp) {
		resp.setHeader("Access-Control-Allow-Origin", "*");
		ServInfo info = new ServInfo();
		info.setState(0);
		info.setMsg("ip或端口号错误");
		if(ip==null||port==null)return info;
		try {
			info = getinfo.getInfo(ip, Integer.valueOf(port));
			info.setState(1);
			return info;
		} catch (NumberFormatException e) {
			info.setMsg("端口号只能是数值");
		} catch (ServerInfoExcption e) {
			info.setMsg(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			info.setMsg("未知错误");
		}
		return info;
	}
}
